package smartcarpool.ui;

import com.example.smartcarpool.R;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.Spinner;

public class tab1_Fragment extends Fragment {
	
	public OnItemSelectedListener item_selected;
	private Spinner spinner1;
	private Spinner spinner2;
	private Spinner spinner3;
	private View view;
	private EditText seats_field;
	private EditText items_field;
    private Intent intent;


	
	public void setListeners(){
		
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.tab1_status);
		Log.d("Information", "Create fragment");
		view = inflater.inflate(R.layout.tab1_status, container, false);
		spinner1 = (Spinner) view.findViewById(R.id.spinner1);
		spinner2 = (Spinner) view.findViewById(R.id.spinner2);
		spinner3 = (Spinner) view.findViewById(R.id.spinner3);
		seats_field = (EditText) view.findViewById(R.id.editText1);
	    items_field = (EditText) view.findViewById(R.id.editText2);

	    //Log.d("Information", "spinner"+spinner1.toString());
	    //Log.d("Information", "try to create listener");
	    SCSpinnerListener listener = new SCSpinnerListener(spinner1, view);
	    SCSpinnerListener listener_spinner2 = new SCSpinnerListener(spinner2, view);
	    //Log.d("Information", "spinner"+spinner1.toString());
	    spinner1.setOnItemSelectedListener(listener);
	    spinner2.setOnItemSelectedListener(listener_spinner2);
	    
	    if (savedInstanceState == null) {
	    	Log.i("Information","no saved bundle");
	    	seats_field.setText("0");
	    	items_field.setText("0");
	    } else {
	    	Log.i("Information","restore values");
	    	seats_field.setText(String.valueOf(savedInstanceState.getInt("freeseats")));
	    	items_field.setText(String.valueOf(savedInstanceState.getInt("freeitems")));
	    }
		return view;
		
		
	}
	@Override
	public void onStart() {
	    super.onStart();
	    Log.i("Information","Start Activity");
	    setRetainInstance(true);
	    
	    
	}
	//@Override
	//public void onViewStateRestored(Bundle savedInstanceState){
	//}
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i("Information", "save bundle");
        outState.putString("Status", spinner1.getSelectedItem().toString());
        outState.putString("Vehicle", spinner2.getSelectedItem().toString());
        Log.d("spin_value", String.valueOf(spinner3.getSelectedItemPosition()));
        outState.putString("Preference", spinner3.getSelectedItem().toString());
        outState.putString("freeseats", seats_field.getText().toString());
        outState.putString("freeitems", items_field.getText().toString());
        //setRetainInstance(true);
        
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    	Log.i("Information", "pause tab1 and save in intent");
        //intent.putExtra("Status", spinner1.getSelectedItem().toString());
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    	super.onActivityCreated(savedInstanceState);
    	Log.i("Information","Create Activity");
    	if (savedInstanceState == null) {
    		Log.i("Information","No saved state");
	    	seats_field.setText("0");
	    	items_field.setText("0");
	    } else {
	    	Log.i("Information","Restore state");
	    	seats_field.setText(savedInstanceState.getInt("freeseats"));
	    	items_field.setText(savedInstanceState.getInt("freeitems"));
	    }
    }
    
}
